
package me.liuhui.mall.manager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import me.liuhui.mall.common.base.vo.ResultVO;
import me.liuhui.mall.manager.runtime.AdminSessionHolder;
import me.liuhui.mall.manager.service.CategoryService;
import me.liuhui.mall.manager.service.convertor.CategoryConverter;
import me.liuhui.mall.manager.service.dto.category.CreateCategoryDTO;
import me.liuhui.mall.manager.service.dto.category.ModifyCategoryDTO;
import me.liuhui.mall.manager.service.vo.category.CategoryVO;
import me.liuhui.mall.repository.mapper.CategoryMapper;
import me.liuhui.mall.repository.model.Category;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Set;

import static me.liuhui.mall.common.utils.Constants.CATEGORY_TOP_PID;

/**
 * Created on 2020/10/14 20:12
 * <p>
 * Description: [TODO]
 * <p>
 *
 * @author [清远]
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Resource
    private CategoryConverter categoryConverter;
    @Resource
    private CategoryMapper categoryMapper;


    @Override
    public ResultVO<List<CategoryVO>> subCategory(Long pid) {
        if(pid == null){
            pid = CATEGORY_TOP_PID;
        }
        LambdaQueryWrapper<Category> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Category::getPid,pid).orderByDesc(Category::getWeight);
        List<Category> categories = categoryMapper.selectList(wrapper);
        return ResultVO.buildSuccessResult(categoryConverter.toVo(categories));
    }

    @Override
    public ResultVO<Boolean> create(CreateCategoryDTO dto) {
        Category entity = categoryConverter.createDtoToEntity(dto);
        entity.setLeaf(true);
        entity.setCreateTime(new Date());
        entity.setCreateAdmin(AdminSessionHolder.getCurrentAdmin().getUsername());
        categoryMapper.insert(entity);
        if (!CATEGORY_TOP_PID.equals(entity.getPid())) {
            Category parent = categoryMapper.selectById(entity.getPid());
            parent.setLeaf(false);
            categoryMapper.updateById(parent);
        }
        return ResultVO.buildSuccessResult();
    }

    @Override
    public ResultVO<Boolean> modify(ModifyCategoryDTO dto) {
        Category category = categoryMapper.selectById(dto.getId());
        if (category == null) {
            return ResultVO.buildFailResult("分类不存在");
        }
        Long oldPid = category.getPid();
        Long newPid = dto.getPid();
        Category entity = categoryConverter.modifyDtoToEntity(dto);
        categoryMapper.updateById(entity);
        if (!Objects.equals(oldPid, newPid)) {
            if (!CATEGORY_TOP_PID.equals(newPid)) {
                Category newParent = categoryMapper.selectById(newPid);
                if (newParent.getLeaf()) {
                    newParent.setLeaf(false);
                    categoryMapper.updateById(newParent);
                }
            }
            if (!CATEGORY_TOP_PID.equals(oldPid)) {
                Category oldParent = categoryMapper.selectById(oldPid);
                LambdaQueryWrapper<Category> wrapper = Wrappers.lambdaQuery();
                wrapper.eq(Category::getPid, oldPid);
                long childNum = categoryMapper.selectCount(wrapper);
                if (childNum == 0) {
                    oldParent.setLeaf(true);
                    categoryMapper.updateById(oldParent);
                }
            }
        }
        return ResultVO.buildSuccessResult();
    }

    @Override
    public ResultVO<Boolean> delete(Set<Long> ids) {
        for (Long id : ids) {
            Category category = categoryMapper.selectById(id);
            if(category == null){
                return ResultVO.buildFailResult("分类不存在");
            }
            categoryMapper.deleteById(id);
            if(!CATEGORY_TOP_PID.equals(category.getPid())) {
                LambdaQueryWrapper<Category> wrapper = Wrappers.lambdaQuery();
                wrapper.eq(Category::getPid, category.getPid());
                long childNum = categoryMapper.selectCount(wrapper);
                if (childNum == 0) {
                    Category parent = new Category();
                    parent.setId(category.getPid());
                    parent.setLeaf(true);
                    categoryMapper.updateById(parent);
                }
            }
        }
        return ResultVO.buildSuccessResult();
    }

}
